home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 402_01 / cforms-2.2 / src / cforms.y < prev    next >
Encoding:
Lex Description  |  1993-07-20  |  12.1 KB  |  541 lines

  1. %{
  2. #include "config.h"
  3. #include "token.h"
  4. #include "comp.h"
  5. extern int yyerror();
  6.  
  7. static int keywords = 1;
  8. static int last_x = 0, last_y = 0 , *last = &last_x;
  9.  
  10. struct {
  11.     struct field field;
  12.     struct viewport viewport;
  13.     struct picture picture;
  14.     struct stmt *stmt;
  15. } current;
  16.  
  17. struct id {
  18.     char space[200];
  19.     char id[200];
  20. };
  21.  
  22. static char *key_lookup(char *key);
  23. static char *event_lookup(char *event);
  24.  
  25. static int yylex(void);
  26.  
  27. %}
  28.  
  29. %token <id> PICTURE FIELD LITERAL RVALUE LVALUE POS CENTER MAX PLUS
  30. %token <id> MINUS TYPE CHAR ID NUMBER LBRACE RBRACE RPAR LPAR COMMA
  31. %token <id> SEMI PROTECTED INVISIBLE FORBIDDEN VIEWPORT CCODE SIZE GLOBAL
  32. %token <id> EVENT KEY INT ALNUM VALUE FORGET UPPERCASE HIGHLIGHT FRAME
  33.  
  34.  
  35. %type <viewport> viewport viewportmembers
  36. %type <picture>     picture picturemembers
  37. %type <field>     field fieldmembers type
  38. %type <literal>     literal
  39. %type <event>     event eventtype
  40. %type <ccode>     ccode
  41. %type <stmt>     stmt intstmt
  42. %type <list>     module
  43. %type <pos>     pos size xcommay
  44. %type <val>     pictureflags fieldflags posval signed number
  45. %type <id>     lvalue rvalue typemember value
  46.  
  47. %union {
  48.     int val;
  49.     struct xy pos;
  50.     struct xy size;
  51.     struct field field;
  52.     struct event event;
  53.     struct picture picture;
  54.     struct id id;
  55.     struct viewport viewport;
  56.     struct stmt *stmt;
  57.     struct ccode ccode;
  58.     struct literal literal;
  59.     struct list list;
  60. }
  61. %%
  62.  
  63. /******************************************************************
  64.  *
  65.  *        M O D U L E
  66.  *
  67.  ******************************************************************/
  68. module: /* Empty */
  69.         {
  70.         $$.viewport = 0;
  71.         }
  72.     | module viewport
  73.         {
  74.         struct viewport *p = memalloc(sizeof *p);
  75.         memcpy(p, &$2, sizeof *p);
  76.         p->link.next = &list.viewport->link;
  77.         list.viewport = p;
  78.         }
  79.     | module picture
  80.         {
  81.         struct picture *p = memalloc(sizeof *p);
  82.         memcpy(p, &$2, sizeof *p);
  83.         p->link.next = &list.picture->link;
  84.         list.picture = p;
  85.         } 
  86.     | module ccode
  87.         {
  88.         struct ccode *p = memalloc(sizeof *p);
  89.         memcpy(p, &$2, sizeof *p);
  90.         p->link.next = &list.ccode->link;
  91.         list.ccode = p;
  92.         }
  93.     | module GLOBAL event
  94.         {
  95.         struct event *p = memalloc(sizeof *p);
  96.         memcpy(p, &$3, sizeof *p);
  97.         p->link.next = &list.event->link;
  98.         list.event = p;
  99.         p->global = 1;
  100.         }
  101.     | module event
  102.         {
  103.         struct event *p = memalloc(sizeof *p);
  104.         memcpy(p, &$2, sizeof *p);
  105.         p->link.next = &list.event->link;
  106.         list.event = p;
  107.         } 
  108.     | module error
  109.         {
  110.         error("syntax error");
  111.         }
  112.     ;
  113.  
  114. /******************************************************************
  115.  *
  116.  *        V I E W P O R T
  117.  *
  118.  ******************************************************************/
  119. viewport: VIEWPORT ID LBRACE viewportmembers RBRACE
  120.     {
  121.         $$.link.name = strduplicate($2.id);
  122.         $$.size = $4.size;
  123.         $$.pos = $4.pos;
  124.     };
  125.  
  126. viewportmembers: /* Empty */
  127.             { $$.pos.x = $$.pos.y = 0;
  128.                $$.size.x = $$.size.y = 0;
  129.             }
  130.     | viewportmembers pos
  131.             {
  132.             $$.pos = $2;
  133.             }
  134.     | viewportmembers size
  135.             {
  136.             $$.size = $2;
  137.             } ;
  138.  
  139. /******************************************************************
  140.  *
  141.  *         P I C T U R E 
  142.  *
  143.  ******************************************************************/
  144.  
  145. picture: PICTURE ID VIEWPORT ID LBRACE picturemembers RBRACE
  146.     {
  147.         struct viewport *vp =
  148.         (struct viewport *)find_name(&list.viewport->link, $4.id);
  149.         if (vp == NULL) {
  150.         error("unknown viewport");
  151.         YYERROR;
  152.         }
  153.         else
  154.         {
  155.         $$.link.name = strduplicate($2.id);
  156.         $$.field = $6.field;
  157.         $$.literal = $6.literal;
  158.         $$.event = $6.event;
  159.         $$.flags = $6.flags;
  160.         $$.viewport = vp;
  161.         }
  162.     };
  163.  
  164. picturemembers: /* Empty */
  165.             {
  166.              $$.field = NULL;
  167.              $$.event = NULL;
  168.              $$.literal = NULL;
  169.              $$.flags = 0;
  170.              }
  171.         | picturemembers field
  172.             {
  173.             struct field *p = memalloc(sizeof *p);
  174.             memcpy(p, &$2, sizeof *p);
  175.             p->link.next = &$$.field->link;
  176.             $$.field = p;
  177.             }
  178.         | picturemembers event
  179.             {
  180.             struct event *p = memalloc(sizeof *p);
  181.             memcpy(p, &$2, sizeof *p);
  182.             p->link.next = &$$.event->link;
  183.             $$.event = p;
  184.             }
  185.         | picturemembers literal
  186.             {
  187.             struct literal *p = memalloc(sizeof *p);
  188.             memcpy(p, &$2, sizeof *p);
  189.             p->link.next = &$$.literal->link;
  190.             $$.literal = p;
  191.             }
  192.         | picturemembers pictureflags
  193.             {
  194.             $$.flags |= $2;
  195.             }
  196.         ;
  197. pictureflags: FRAME SEMI
  198.     {
  199.         $$ = PIC_FRAME;
  200.     }
  201.     ;
  202.  
  203.  
  204. /******************************************************************
  205.  *
  206.  *        F I E L D
  207.  *
  208.  ******************************************************************/
  209. field:  FIELD ID LBRACE {current.field.flags = 0; } fieldmembers RBRACE 
  210.     {
  211.         $$.link.next = NULL;
  212.         $$.link.name = strduplicate($2.id);
  213.         $$.pos = $5.pos;
  214.         $$.event = $5.event;
  215.         $$.lvalue = $5.lvalue;
  216.         $$.rvalue = $5.rvalue;
  217.         $$.type = $5.type;
  218.         $$.value = $5.value;
  219.         $$.len = $5.len;
  220.         $$.flags = $5.flags;
  221.  
  222.         if ($$.type == NULL) {
  223.         error("unknown type for field");
  224.  
  225.         if ($$.lvalue) free($$.lvalue);
  226.         if ($$.rvalue) free($$.rvalue);
  227.         if ($$.type) free($$.type);
  228.         if ($$.value) free($$.value);
  229.         YYERROR;
  230.         }
  231.     } ;
  232.  
  233. fieldmembers: /* Empty */
  234.           {
  235.               $$.pos.x = 0;
  236.               $$.pos.y = 0;
  237.               $$.event = NULL;
  238.               $$.lvalue = NULL;
  239.               $$.rvalue = NULL;
  240.               $$.type = NULL;
  241.               $$.value = NULL;
  242.               $$.len = 0;
  243.               $$.flags = 0;
  244.           }
  245.           | fieldmembers pos
  246.           { $$.pos = $2; }
  247.           | fieldmembers event
  248.           {
  249.             struct event *p = memalloc(sizeof *p);
  250.             memcpy(p, &$2, sizeof *p);
  251.             p->link.next = &$$.event->link;
  252.             $$.event = p;
  253.           }
  254.           | fieldmembers lvalue
  255.           { $$.lvalue = strip_quotes(strduplicate($2.id)); }
  256.           | fieldmembers rvalue
  257.           { $$.rvalue = strip_quotes(strduplicate($2.id)); }
  258.           | fieldmembers type
  259.           {
  260.               $$.type = $2.type ;
  261.               $$.len  = $2.len;
  262.           }
  263.           | fieldmembers value
  264.           { $$.value = strip_quotes(strduplicate($2.id)); }
  265.           | fieldmembers fieldflags
  266.           { $$.flags |= $2; }
  267.           | fieldmembers error SEMI
  268.           {
  269.               error("syntax error for field");
  270.               yyerrok;
  271.           }
  272.           ;
  273.  
  274. fieldflags:   PROTECTED SEMI
  275.         { $$ = FLD_PROTECTED; }
  276.         | FORBIDDEN SEMI
  277.         { $$ = FLD_FORBIDDEN; }
  278.         | UPPERCASE SEMI
  279.         { $$ = FLD_UPPERCASE; }
  280.         | HIGHLIGHT SEMI
  281.         { $$ = FLD_HIGHLIGHT; }
  282.         | INVISIBLE SEMI
  283.         { $$ = FLD_INVISIBLE; };
  284.  
  285. lvalue: LVALUE ID SEMI
  286.     { $$ = $2; };
  287.  
  288. rvalue: RVALUE ID SEMI
  289.     { $$ = $2; };
  290.  
  291. type: TYPE typemember SEMI
  292.     { $$.type = strduplicate($2.id); }
  293.     | TYPE typemember LPAR number RPAR SEMI
  294.     {
  295.         $$.type = strduplicate($2.id);
  296.         $$.len = $4;
  297.     } ;
  298.  
  299. typemember:   CHAR
  300.         { strcpy($$.id, "FLD_CHAR"); };
  301.         | ALNUM
  302.         { strcpy($$.id, "FLD_ALNUM"); }
  303.         | INT
  304.         { strcpy($$.id, "FLD_INT"); };
  305.     
  306. value: VALUE ID SEMI
  307.     { $$ = $2; };
  308.  
  309.  
  310. /******************************************************************
  311.  *
  312.  *        L I T E R A L
  313.  *
  314.  ******************************************************************/
  315. literal: LITERAL xcommay COMMA ID SEMI
  316.     {
  317.     $$.link.next = NULL;
  318.     $$.link.name = strip_quotes(strduplicate($4.id));
  319.     $$.pos = $2;
  320.     $$.flags = 0;
  321.     } ;
  322.  
  323.  
  324. /******************************************************************
  325.  *
  326.  *        E V E N T
  327.  *
  328.  ******************************************************************/
  329. event:   EVENT eventtype LBRACE stmt RBRACE
  330.         {
  331.         $$.link.next = NULL;
  332.         $$.link.name = $2.link.name;
  333.         $$.type = $2.type;
  334.         $$.code = $2.code;
  335.         $$.global = 0;
  336.         $$.stmt = $4;
  337.         }
  338.     | EVENT eventtype FORGET SEMI
  339.         {
  340.         $$.link.next = NULL;
  341.         $$.link.name = $2.link.name;
  342.         $$.type = $2.type;
  343.         $$.code = $2.code;
  344.         $$.global = 0;
  345.         $$.stmt = NULL;
  346.         }
  347.     | EVENT error 
  348.         {
  349.         $$.link.next = NULL;
  350.         $$.stmt = NULL;
  351.         error("syntax error for event");
  352.         };
  353.  
  354. eventtype: /* Empty */
  355.         {
  356.             $$.type = NULL;
  357.             $$.code = NULL;
  358.         }
  359.        | KEY ID
  360.         {
  361.             char name[100];
  362.             sprintf(name, "key_%s", $2.id);
  363.             $$.link.name = strtoupper(strduplicate(name));
  364.             $$.type = strduplicate("EVENT_KEY");
  365.             $$.code = key_lookup($2.id);
  366.             if ($$.code != NULL) {
  367.             $$.code = strduplicate($$.code);
  368.             }
  369.             else {
  370.             error("unknown key");
  371.             YYERROR;
  372.             }
  373.  
  374.         }
  375.        | ID
  376.         {
  377.             char name[100];
  378.             sprintf(name, "EVENT_%s", $1.id);
  379.             $$.link.name = strtoupper(strduplicate($1.id));
  380.             $$.type = strtoupper(strduplicate(name));
  381.             $$.code = strduplicate("0");
  382.             if (event_lookup($1.id) == NULL) {
  383.             error("unknown event");
  384.             YYERROR;
  385.             }
  386.         }
  387.         ;
  388.  
  389.  
  390. /******************************************************************
  391.  *
  392.  *         C C O D E
  393.  *
  394.  ******************************************************************/
  395. ccode: CCODE LBRACE stmt RBRACE
  396.     {
  397.         $$.link.next = NULL;
  398.         $$.stmt = $3;
  399.     }
  400.     | CCODE error RBRACE
  401.     {
  402.         $$.link.next = NULL;
  403.         $$.stmt = NULL;
  404.         error(" ccode");
  405.     };
  406.  
  407. /******************************************************************
  408.  *
  409.  *        Others
  410.  *
  411.  ******************************************************************/
  412. pos: POS xcommay SEMI
  413.     { $$ = $2; }
  414.  
  415. xcommay: {last = &last_x;} posval COMMA {last = &last_y;} posval
  416.     { last_x = $$.x = $2; last_y = $$.y = $5; } ;
  417.  
  418. size: SIZE posval COMMA posval SEMI
  419.     { $$.x = $2; $$.y = $4; } ;
  420.  
  421. posval:   signed
  422.     | CENTER { $$ = CF_CENTER; }
  423.     | MAX { $$ = CF_MAX; } ;
  424.  
  425. signed:   number {$$ = $1;}
  426.     | PLUS number {$$ = *last  + $2;}
  427.     | MINUS number {$$ = *last - $2;} ;
  428.  
  429. stmt: { current.stmt = NULL; keywords = 0; $$ = NULL; } intstmt
  430.     {
  431.         keywords=1; $$ = current.stmt;
  432.     } ;
  433. intstmt: /* Empty */
  434.         { $$ = NULL; }
  435.        | intstmt ID
  436.         {
  437.         stmt_add(¤t.stmt, $2.id, $2.space);
  438.         }
  439.        | intstmt LBRACE {stmt_add(¤t.stmt, "{", $2.space);} intstmt RBRACE { stmt_add(¤t.stmt, "}", $5.space); }
  440.        ;
  441.  
  442. number: NUMBER { $$ = atoi($1.id); };
  443. %%
  444.  
  445. static int yylex(void)
  446. {
  447.     static char token[500];
  448.     static char space[500];
  449. #if YYDEBUG != 0
  450.     extern int yydebug;
  451.     yydebug = 1;
  452. #endif
  453.     if (GetTok(token, space) == NULL) return -1;
  454.     strcpy(yylval.id.id, token);
  455.     strcpy(yylval.id.space, space);
  456.  
  457.     if (keywords) {
  458.     if (strequ(token, "VIEWPORT") == 0) return VIEWPORT;
  459.     if (strequ(token, "CCODE") == 0) return CCODE;
  460.     if (strequ(token, "PICTURE") == 0) return PICTURE;
  461.     if (strequ(token, "FIELD") == 0) return FIELD;
  462.     if (strequ(token, "PROTECTED") == 0) return PROTECTED;
  463.     if (strequ(token, "FORBIDDEN") == 0) return FORBIDDEN;
  464.     if (strequ(token, "INVISIBLE") == 0) return INVISIBLE;
  465.     if (strequ(token, "UPPERCASE") == 0) return UPPERCASE;
  466.     if (strequ(token, "HIGHLIGHT") == 0) return HIGHLIGHT;
  467.     if (strequ(token, "FRAME") == 0) return FRAME;
  468.     if (strequ(token, "VALUE") == 0) return VALUE;
  469.     if (strequ(token, "LITERAL") == 0) return LITERAL;
  470.     if (strequ(token, "LVALUE") == 0) return LVALUE;
  471.     if (strequ(token, "RVALUE") == 0) return RVALUE;
  472.     if (strequ(token, "EVENT") == 0) return EVENT;
  473.     if (strequ(token, "KEY") == 0) return KEY;
  474.     if (strequ(token, "POS") == 0) return POS;
  475.     if (strequ(token, "CENTER") == 0) return CENTER;
  476.     if (strequ(token, "MAX") == 0) return MAX;
  477.     if (strequ(token, "+") == 0) return PLUS;
  478.     if (strequ(token, "-") == 0) return MINUS;
  479.     if (strequ(token, "TYPE") == 0) return TYPE;
  480.     if (strequ(token, "CHAR") == 0) return CHAR;
  481.     if (strequ(token, "ALNUM") == 0) return ALNUM;
  482.     if (strequ(token, "SIZE") == 0) return SIZE;
  483.     if (strequ(token, "GLOBAL") == 0) return GLOBAL;
  484.     if (strequ(token, "INT") == 0) return INT;
  485.     if (strequ(token, ",") == 0) return COMMA;
  486.     if (strequ(token, ";") == 0) return SEMI;
  487.     if (strequ(token, "(") == 0) return LPAR;
  488.     if (strequ(token, ")") == 0) return RPAR;
  489.     if (strequ(token, "KEY") == 0) return KEY;
  490.     if (strequ(token, "FORGET") == 0) return FORGET;
  491.  
  492.     if (isdigit(token[0])) return NUMBER;
  493.  
  494.     }
  495.     if (strequ(token, "{") == 0) return LBRACE;
  496.     if (strequ(token, "}") == 0) return RBRACE;
  497.  
  498.     return ID;
  499. }
  500.  
  501. int
  502. yyerror(const char *txt)
  503. {
  504.     return 0;
  505. }
  506.  
  507. static char *
  508. key_lookup(char *key)
  509. {
  510.     static struct {
  511.     char *key;
  512.     char *code;
  513.     } keys[] = {
  514. #include "keys.h"
  515.     };
  516.     int i;
  517.  
  518.     for (i = 0; i < (sizeof keys / sizeof keys[0]); i++)
  519.     {
  520.     if (strequ(keys[i].key, key) == 0) return keys[i].code;
  521.     }
  522.     return NULL;
  523. }
  524.  
  525. static char *
  526. event_lookup(char *event)
  527. {
  528.     static struct {
  529.     char *event;
  530.     } events[] = {
  531. #include "events.h"
  532.     };
  533.     int i;
  534.  
  535.     for (i = 0; i < (sizeof events / sizeof events[0]); i++)
  536.     {
  537.     if (strequ(events[i].event, event) == 0) return events[i].event;
  538.     }
  539.     return NULL;
  540. }
  541.